package com.heima.controller;

import com.heima.annotation.OperationFill;
import com.heima.dto.InsertBusinessDto;
import com.heima.dto.OpportunityDto;
import com.heima.dto.OpportunitySelectDto;
import com.heima.dto.pagedto.PageOpportunityDto;
import com.heima.result.Result;
import com.heima.service.OpportunityService;
import com.heima.vo.*;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@Slf4j
@Api(tags = "商机模块")
public class OpportunityController {

    private final OpportunityService opportunityService;

    /**
     * 商机分页查询
     */
    @GetMapping("/business/list")
    @OperationFill
    public PageResult BusinessList(PageOpportunityDto dto,
                                   @RequestParam(value = "params[beginCreateTime]",required = false) String begin,
                                   @RequestParam(value = "params[endCreateTime]",required = false) String end){
        log.info("商机分页查询:{}",dto);
        dto.setBeginCreateTime(begin);
        dto.setEndCreateTime(end);
        PageResult vo = opportunityService.queryOpportunity(dto);
        return vo;
    }

    /**
     * 新增商机
     */
    @PostMapping("/business")
    @OperationFill
    public ResultVo insertBusiness(@RequestBody  InsertBusinessDto dto){
        log.info("新增商机：{}",dto);
        opportunityService.insertBusiness(dto);
        return new ResultVo();
    }

    /**
     * 根据id查询商机的信息
     */
    @GetMapping("/business/{id}")
    @OperationFill
    public BusinessIdListVo findBusinessById(@PathVariable Integer id){
        log.info("根据id查询商机信息：{}",id);
        BusinessIdListVo vo = opportunityService.findBusinessById(id);
        return vo;
    }

    /**
     * 获取商机沟通历史
     */
    @GetMapping("/business/record/list")
    public BusinessIdInfoListVo businessIdInfoVo(Integer businessId){
        log.info("获取商机沟通历史:{}",businessId);
        BusinessIdInfoListVo vo = opportunityService.businessIdInfoVo(businessId);
        return vo;
    }



    @PostMapping("/business/record")
    public ResultVo recordBusiness2(@RequestBody OpportunityDto dto){
        log.info("跟进商机并修改:{}",dto);
        opportunityService.recordBusiness(dto);
        return new ResultVo();
    }

    /**
     * 查询商机的跟进详情
     */
    @GetMapping("/business/pool")
    @OperationFill
    public PageResult BusinessPool(PageOpportunityDto dto,
                                   @RequestParam(value = "params[beginCreateTime]",required = false) String begin,
                                   @RequestParam(value = "params[endCreateTime]",required = false) String end){
        log.info("商机分页查询:{}",dto);
        dto.setBeginCreateTime(begin);
        dto.setEndCreateTime(end);
        PageResult vo = opportunityService.queryPool(dto);
        return vo;
    }

    /**
     * 商机分配
     * @return
     */
    @PutMapping("/business/assignment")
    public Result assignment(@RequestBody OpportunitySelectDto opportunitySelectDto) {
        opportunityService.assignment(opportunitySelectDto);
        return Result.success();
    }

}
